
<script type="text/javascript" src="<{$web_root}>/js/libs/ajaxfileupload.js"></script>
<script>
function validate_size_img(ele){
    // 返回 KB，保留小数点后两位
    //alert((ele.files[0].size/(1024*1024)).toFixed(2));
    var file = ele.value;
    //alert((ele.files[0].size).toFixed(2));
    //返回Byte(B),保留小数点后两位
    if(((ele.files[0].size).toFixed(2)) >= (1*1024*1024)){
        alert("请上传小于1M的文件");
        return false;
    }
    return true;
}
function ajaxFileUpload($file_obj) {
    $file_id = $file_obj.attr("file_id");
    $target_id = $file_obj.attr("target_id");
    $thumb_width = $file_obj.attr("thumb_width");
    $thumb_height = $file_obj.attr("thumb_height");
    $allow_ext = $file_obj.attr("allow_ext");
    $args = $file_obj.attr("args");
    $.ajaxFileUpload({
        url: '<{spUrl c=$G__C a=ajaxfile}>&file=' + $file_id + '&thumb_width=' + $thumb_width + '&thumb_height=' + $thumb_height + '&allow_ext=' + $allow_ext+'&args='+$args,
        secureuri: false,
        fileElementId: $file_id,
        dataType: 'json',
        success: function (json, status){
        	if(json.result == 0) {
				alert(json.msg);
        	}else if(json.result == 1){
        		$("#" + $target_id).val(json.imgurl);
        		$("#" + $target_id + "_view").attr("href", "../" + json.imgurl).removeClass("zxhide");
        	}
        	hideBlockMsg('.zxblockui');
        },
        error: function (data, status, e){
            alert(e);
            hideBlockMsg('.zxblockui');
        }
    });
    return false;
}
function ajaxFileUpload_auto($file_obj) {
    $upload_id = $file_obj.attr("id");
    $target_id = $upload_id.substr(0, $upload_id.indexOf("_upload"));
    $thumb_width = $(this).attr("thumb_width");
    $thumb_height = $(this).attr("thumb_height");
    $allow_ext = $(this).attr("allow_ext");
    $args = $(this).attr("args");
    $.ajaxFileUpload({
        url: '<{spUrl c=$G__C a=ajaxfile}>&file=' + $file_obj.attr("name") + '&thumb_width=' + $thumb_width + '&thumb_height=' + $thumb_height + '&allow_ext=' + $allow_ext+'&'+$args,
        secureuri: false,
        fileElementId: $upload_id,
        dataType: 'json',
        success: function (json, status){
            if(json.result == 0) {
                alert(json.msg);
            }else if(json.result == 1){
                $("#" + $target_id).val(json.imgurl);
                $("#" + $target_id + "_view").attr("src", "../" + json.imgurl);
                $("#" + $target_id + "_view").parent().removeClass("zxhide");
            }
            hideBlockMsg('.zxblockui');
        },
        error: function (data, status, e){
            alert(e);
            hideBlockMsg('.zxblockui');
        }
    });
    return false;
}
$(function(){
	$(".field_upload").click(function(){
		showBlockMsg(".zxblockui", "");
        if(validate_size_img(this))
            ajaxFileUpload($(this));
        else
            hideBlockMsg('.zxblockui');
	});
    $(".field_upload_auto").live('change', function(){
        showBlockMsg(".zxblockui", "");
        if(validate_size_img(this)){
            ajaxFileUpload_auto($(this));
        }else
            hideBlockMsg('.zxblockui');
    });
})
</script>